﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Text;

namespace Common
{
    /// <summary>
    /// 日志记录(文件)
    /// </summary>
    public class LogHelper
    {
        public static string lOG_PATH = Directory.GetCurrentDirectory()+ "/LOG/";
        private static int LOG_LEVENL = 3;



        /// <summary>
        /// 向日志文件写入调试信息
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="content">写入内容</param>
        public static void Debug(string className, string content)
        {
            if (LOG_LEVENL >= 3)
            {
                WriteLog("DEBUG", className, content);
            }
        }

        /// <summary>
        /// 向日志文件写入运行时信息
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="content">写入内容</param>
        public static void Info(string className, string content)
        {
            if (LOG_LEVENL >= 2)
            {
                WriteLog("INFO", className, content);
            }
        }

        /// <summary>
        /// 向日志文件写入出错信息
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="content">写入内容</param>
        public static void Error(string className, string content)
        {
            if (LOG_LEVENL >= 1)
            {
                WriteLog("ERROR", className, content);
            }
        }

        /// <summary>
        /// 向日志文件写入出错信息
        /// </summary>
        /// <param name="filename">自定义日志文件名称</param>
        /// <param name="className">类名</param>
        /// <param name="content">写入内容</param>
        public static void Error(string filename,string className, string content)
        {
            if (LOG_LEVENL >= 1)
            {
                WriteLog(filename,"ERROR", className, content);
            }
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="type">日志类别</param>
        /// <param name="className">类名</param>
        /// <param name="Message">日志内容</param>
        private static void WriteLog(string type, string className, string Message)
        {
            DateTime dt = DateTime.Now;
            string logPath = lOG_PATH + dt.ToString("yyyy-MM");
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }
            StreamWriter mySw = null;
            try
            {
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
                string filename = logPath + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
                string write_content = time + " " + type + " " + className + ": " + Message;
                
                //创建或打开日志文件，向日志文件末尾追加记录
                mySw = File.AppendText(filename);

                //向日志文件写入内容
                mySw.WriteLine(write_content);

                //关闭日志文件
                mySw.Dispose();

            }
            catch
            {
                throw new Exception("写入日志报错");
            }
            finally
            {
                if (mySw != null)
                {
                    mySw.Dispose();
                }
            }
        }

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="filename">自定义日志文件名称</param>
        /// <param name="type">日志类别</param>
        /// <param name="className">类名</param>
        /// <param name="Message">日志内容</param>
        public static void WriteLog(string filename, string type, string className, string Message)
        {
            DateTime dt = DateTime.Now;
            string logPath = lOG_PATH +  dt.ToString("yyyy-MM");
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }
            StreamWriter mySw = null;
            try
            {
                string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
                string filenamepath = logPath + "/" + filename + ".log";//日志文件命名
                string write_content = time + " " + type + " " + className + ": " + Message;
                
                //创建或打开日志文件，向日志文件末尾追加记录
                mySw = File.AppendText(filenamepath);

                //向日志文件写入内容
                mySw.WriteLine(write_content);

                //关闭日志文件
                mySw.Dispose();

            }
            catch
            {
                throw new Exception("写入日志报错");
            }
            finally
            {
                if (mySw != null)
                {
                    mySw.Dispose();
                }
            }
        }

    }
}
